package com.xx.ems.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.xx.ems.model.Roles;
import com.xx.ems.model.Users;
import com.xx.ems.system.BaseController;

/**
 * @author xxx
 * @date 2014-2-27
 * @version 1.0
 * 描述: 权限管理类
 */
@Controller
@RequestMapping(value="permissions",method=RequestMethod.POST)
@SessionAttributes(types=Users.class,value="loginuser")
public class PermissionsController extends BaseController {
	/**
	 * 描述: 添加权限
	 * @param actionids  选择的所有操作id
	 * @param role
	 * @param response
	 * @param user
	 * @date 2014-2-27
	 * 
	 * json格式：
	 * var data = {actionids:"2132312,23232,233232",rolename:"32434"};
	 * $.ajax({
	 * 		url:"/ems/permissions/addPermissons",
	 * 		type:"POST",
	 * 		data:data,
	 * 		dataType:"json",
	 * 		success:function(data){
	 * 			alert(data.result);
	 * 		}
	 * });
	 */
	@RequestMapping(value="/addPermissons")
	public void addPermissons(String actionids, Roles role, HttpServletResponse response, @ModelAttribute("loginuser") Users user) {
		try{
			boolean result = getPermissionsService().addPermissons(actionids, role, user);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 修改权限 
	 * @param oldactionids  旧的操作id
	 * @param newactionids  新选择的操作id
	 * @param role
	 * @param response
	 * @param user
	 * @date 2014-2-27
	 */
	@RequestMapping(value="/updatePermissions")
	public void updatePermissions(String oldactionids, String newactionids, Roles role, HttpServletResponse response, @ModelAttribute("loginuser") Users user) {
		try{
			boolean result = getPermissionsService().updatePermissions(oldactionids, newactionids, role, user);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 删除权限 
	 * @param roleid
	 * @param response
	 * @date 2014-2-27
	 */
	@RequestMapping(value="/deletePermissions")
	public void deletePermissions(String roleid, HttpServletResponse response) {
		try{
			boolean result = getPermissionsService().deletePermissions(roleid);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 将指定的权限授权给用户 
	 * @param userids
	 * @param roleid
	 * @param response
	 * @date 2014-3-1
	 */
	@RequestMapping(value="/authorizeToUsers")
	public void authorizeToUsers(String userids, String roleid, HttpServletResponse response) {
		try{
			boolean result = getPermissionsService().authorizeToUsers(userids, roleid);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 获取全部权限 (除了权限管理部分)
	 * @param response
	 * @date 2014-3-1
	 * 
	 * json格式：{"result":[{a1:"1",b1:"2"},{a2:"11",b2:"22"}]}
	 * 前台如果是用参数(如：data)来接受数据,则可以直接通过 data.result[0].a1 这种形式来获取 a1 的值
	 */
	@RequestMapping(value="/getAllPermissions")
	public void getAllPermissions(HttpServletResponse response) {
		try{
			List<Object> actionList = getPermissionsService().getAllPermissions();
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			json.put("result", actionList);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 获取角色列表 
	 * @param pageNow
	 * @param maxCount
	 * @param response
	 * @date 2014-3-4
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getRolesList")
	public void getRolesList(@RequestParam(defaultValue="1") int pageNow, @RequestParam(defaultValue="8") int maxCount, HttpServletResponse response) {
		try{
			Map<String, Object> map = getPermissionsService().getRolesList(pageNow, maxCount);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			List<Object> list = (List<Object>) map.get("list");
			if (list.size() > 0) {
				json.put("result", list);
			}else {
				json.put("result", "empty");
			}
			json.put("pageNow", pageNow);
			json.put("totalpage", map.get("totalpage"));
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 根据角色id获取相应的权限信息 
	 * @param rid
	 * @param response
	 * @date 2014-3-4
	 */
	@RequestMapping(value="/getActionsByRoleid")
	public void getActionsByRoleid(int rid, HttpServletResponse response) {
		try{
			List<Object> list = getPermissionsService().getActionsByRoleid(rid);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (list.size() > 0) {
				json.put("result", list);
			}else {
				json.put("result", "empty");
			}
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}